<?php 

class WC_Meta_boxes
{

	var $_fields = '';

	function __construct()
	{

		add_action( 'admin_init', array( $this, 'add_metabox' ) );

		add_action('admin_enqueue_scripts', array($this, 'enqueue') );

		add_action( 'save_post', array( $this, 'publish_post' ) );

	}

	

	function enqueue()

	{

		wp_register_script('jquery-select2', get_template_directory_uri().'/libs/metaboxes/js/jquery.select2.min.js');

		wp_register_style('jquery-ui-datepicker-custom', get_template_directory_uri().'/libs/metaboxes/css/jquery.ui.all.css');

		wp_register_style('admin-custom-styles', get_template_directory_uri().'/libs/metaboxes/css/style.css');

	}

	

	function add_metabox()

	{
		
		include(get_template_directory().'/libs/metaboxes/settings.php');

		$this->_fields = $settings;

		$keys = array_keys( $this->_fields );

		//printr($keys);

		foreach( $keys as $k ){

			add_meta_box($k.'_settings', sprintf(__( '%s Settings', SH_NAME ), ucwords(str_replace( array('wc_', '_'), ' ', $k ))), array( $this, 'inner_custom_box'), $k );			

		}

	}

	

	function inner_custom_box($post)

	{

		wp_enqueue_style(array('jquery-ui-datepicker-custom', 'admin-custom-styles'));

		wp_enqueue_script(array('jquery-ui-datepicker', 'jquery-select2'));

		$t = $GLOBALS['_webnukes'];
		$t->load('html_class');
		$post_type = kvalue( $post, 'post_type');

		

		$settings = get_post_meta( kvalue($post, 'ID' ), '_'.$post_type.'_settings', true);

		//printr($settings);

		//$fields = $t->_fields_enqueue(kvalue($this->_fields, $post_type), $settings); 

		//printr($fields);

		$fields = kvalue( $this->_fields, $post_type );

		//printr($fields);

		//printr($nph);

		if( $fields && is_array( $fields ) ): ?>

        	<script type="text/javascript">

			jQuery(document).ready(function($) {

				$('.fields_set select').select2();

				if( $('#start_date').length > 0 ){

					$('#start_date, #end_date').datepicker();

				}

			});

			</script>



			<?php 	foreach( $fields as $f):?>

                <div class="fields_set" >

                	<label><strong><?php echo kvalue( $f, 'title'); ?></strong></label>

                    <div class="field">

                    	<?php if( $n_field = kvalue( $f, 'fields' ) ): ?>
                        	<?php foreach( $n_field as $n_f ): ?>
                            	<?php echo $t->html->generator(kvalue( $n_f, 'id' ),  $n_f, $settings);	?>
                            <?php endforeach; ?>
                        <?php else: ?>
							<?php echo $t->html->generator(kvalue( $f, 'id' ),  $f, $settings);	?>
                        <?php endif; ?>

                    </div>

                </div>

            <?php endforeach;

		endif;

	}

	

	

	function _html()

	{

	}

	

	function publish_post($post_id)

	{

		global $post;

		$post_type = kvalue( $_POST, 'post_type' );

		$setting = kvalue($this->_fields, $post_type);

		

		$types = array_merge(array('post', 'page'), array_keys( $this->_fields )); 

		if( !in_array($post_type , $types) ) return;

		

		$data = kvalue( $_POST, $post_type);//array_intersect_key( $_POST, $setting);

		if( !$data ) return;

		//printr($post);

		if( $post_type == 'dict_event') update_post_meta( $post_id, '_dict_event_date', kvalue( $data, 'start_date'));

		update_post_meta( $post_id, '_'.$post_type.'_settings', $data );

	}

}


new WC_Meta_boxes;


